const router = require('koa-router')()
const { getWebAccessToken, getCustomer, sendCodeAction, addQuestionAction, wechatTelAction, isFlagAction, reactDataAction, isOkAction, reactListAction, reactdownAction, reactcityAction } = require('../servers/index');

router.prefix('/api')

//获取小程序的openid
router.post('/code', async (ctx, next) => {
  const { code,area } = ctx.request.body;
  console.log(ctx.request.body);
  console.log('code===', code, 'area===', area);
  const data = await getWebAccessToken(code, area);
  ctx.body = data;
})


//验证客户代码是正确性
router.post('/customer', async (ctx, next) => {
  const { customer ,area} = ctx.request.body
  const data = await getCustomer(customer, area);
  ctx.body = data;
})


//发送短信验证码
router.get('/sendCode', async (ctx, next) => {
  const data = ctx.request.query;
  const result = await sendCodeAction(data);
  ctx.body = result;
})

//问答结果提交（客户代码，手机号，问答结果，地理位置信息
router.post('/addQuestion', async (ctx, next) => {
  const result = await addQuestionAction(ctx);
  ctx.body = result;
})

//验证短信验证码
router.post('/wechatTel', async (ctx, next) => {
  const result = await wechatTelAction(ctx);
  ctx.body = result;
})


//是否已参与活动判断
router.get('/isFlag', async (ctx, next) => {
  const data = ctx.request.query;
  const result = await isFlagAction(data);
  ctx.body = result;
})

//埋点记录表
router.post('/isOk', async (ctx, next) => {
  const result = await isOkAction(ctx);
  ctx.body = result;
})

//数据统计接口---10个数据
router.get('/reactData', async (ctx, next) => {
  const { area } = ctx.request.query;
  console.log('react==area==',area);
  if (area){
    const result = await reactDataAction(area);
    ctx.body = result;
  }else{
    ctx.body = { "code": 300, "data": "请传入合法有效的城市" };
  }
})
//数据统计接口----上海列表
router.get('/reactList', async (ctx, next) => {
  const { area, customer } = ctx.request.query;
  console.log('react==area==', area, 'customer==', customer);
  if (area) {
    const result = await reactListAction(area, customer);
    ctx.body = result;
  } else {
    ctx.body = { "code": 300, "data": "请传入合法有效的城市" };
  }
})
//下载导出表格
router.get('/reactdown', async (ctx, next) => {
  const { area, customer } = ctx.request.query;
  console.log('react==area==', area, 'customer==', customer);
  if (area) {
    const result = await reactdownAction(area, customer);
    ctx.body = result;
  } else {
    ctx.body = { "code": 300, "data": "请传入合法有效的城市" };
  }
})

//城市登录
router.get('/reactcity', async (ctx, next) => {
  const { pwd } = ctx.request.query;
  console.log('react==pwd==', pwd);
  if (pwd) {
    const result = await reactcityAction(pwd);
    ctx.body = result;
  } else {
    ctx.body = { "code": 300, "data": "请输入正确的城市密码" };
  }
})

module.exports = router
